package uestc.lj.midPromotion.macro;

/**
 * 有序数组中寻找是否存在k值
 * 每一行每一列从左往右和从上往下递增
 *
 * @Author:Crazlee
 * @Date:2021/11/30
 */
public class Code04_FindNumInSortedMatrix {
	public static boolean isContains(int[][] matrix, int K) {
		int row = 0;
		int col = matrix[0].length - 1;

		while (row < matrix.length && col > -1) {
			if (matrix[row][col] == K) {
				return true;
			} else if (matrix[row][col] > K) {
				col--;
			} else {
				row++;
			}
		}
		return false;
	}

	public static void main(String[] args) {
		int[][] matrix = new int[][]{
				// 0
				{0, 1, 2, 3, 4, 5, 6},
				// 1
				{10, 12, 13, 15, 16, 17, 18},
				// 2
				{23, 24, 25, 26, 27, 28, 29},
				// 3
				{44, 45, 46, 47, 48, 49, 50},
				// 4
				{65, 66, 67, 68, 69, 70, 71},
				// 5
				{96, 97, 98, 99, 100, 111, 122},
				// 6
				{166, 176, 186, 187, 190, 195, 200},
				// 7
				{233, 243, 321, 341, 356, 370, 380}
		};
		int K = 233;
		System.out.println(isContains(matrix, K));
	}

}

